import {
  AbilityType,
  APIType,
  APIVersion,
  LanguageType,
  ModuleTemplateType,
  RuntimeOSType
} from '../../../../../models/constants';
import { ModuleTemplateOption, ModuleTemplateOptionBuilder } from '../../../../../models/model-options';
import { ModuleConfigInitFun } from '../../../../../models/module-models';
import { copyTmp, DirTemplateCreator, dirTmp, renderTmp } from '../../../../../models/template-models';
import { entryBuilder, moduleBuilder } from '../../../../../module-builder';

/**
 * EmptyAbility(entry)
 *
 * @type {ModuleTemplateOption}
 */
const entryOption: ModuleTemplateOption = ModuleTemplateOptionBuilder.builder()
  .abilityType(AbilityType.AtomicService)
  .apiType(APIType.FA)
  .apiVersion(APIVersion.API10)
  .language(LanguageType.ArkTs)
  .moduleTemplateType(ModuleTemplateType.EmptyAbility)
  .runtimeOS(RuntimeOSType.HarmonyOS)
  .build();

const entryTemplate: DirTemplateCreator = (dirName) => dirTmp(dirName)
  .add(dirTmp('src')
    .add(dirTmp('main')
      .add(copyTmp('/module/fa/service/common/src/main/ets'))
      .add(copyTmp('/module/fa/service/common/src/main/js'))
      .add(copyTmp('/module/fa/service/common/src/main/resources'))
      .add(renderTmp('/entry/fa/service/emptyAbility/api10/src/main/config.json'))
    )
    .add(dirTmp('ohosTest')
      .add(copyTmp('/module/fa/service/common/src/ohosTest/ets'))
      .add(copyTmp('/module/fa/service/common/src/ohosTest/resources'))
      .add(renderTmp('/module/fa/service/common/src/ohosTest/config.json'))
    )
  )
  .add(renderTmp('/entry/fa/service/emptyAbility/api10/build-profile.json5'))
  .add(copyTmp('/entry/fa/service/emptyAbility/api10/hvigorfile.ts'))
  .add(renderTmp('/entry/fa/service/emptyAbility/api10/oh-package.json5'));


const initFun: ModuleConfigInitFun = async (name, option, template) => {
  template.node('src').node('main').node('config.json').data = { moduleName: name };
  template.node('src').node('ohosTest').node('config.json').data = { moduleName: name };
  template.node('build-profile.json5').data = { apiType: option.apiType };
  template.node('oh-package.json5').data = { moduleName: name };
};

entryBuilder.add(entryOption, entryTemplate, initFun);

/**
 * EmptyAbility
 *
 * @type {ModuleTemplateOption}
 */
const emptyAbilityOption: ModuleTemplateOption = ModuleTemplateOptionBuilder.builder()
  .abilityType(AbilityType.AtomicService)
  .apiType(APIType.FA)
  .apiVersion(APIVersion.API10)
  .language(LanguageType.ArkTs)
  .moduleTemplateType(ModuleTemplateType.EmptyAbility)
  .runtimeOS(RuntimeOSType.HarmonyOS)
  .build();

const entryAbilityTemplate: DirTemplateCreator = (dirName) => dirTmp(dirName)
  .add(dirTmp('src')
    .add(dirTmp('main')
      .add(copyTmp('/module/fa/service/common/src/main/ets'))
      .add(copyTmp('/module/fa/service/common/src/main/js'))
      .add(copyTmp('/module/fa/service/common/src/main/resources'))
      .add(renderTmp('/module/fa/service/emptyAbility/api10/src/main/config.json'))
    )
    .add(dirTmp('ohosTest')
      .add(copyTmp('/module/fa/service/common/src/ohosTest/ets'))
      .add(copyTmp('/module/fa/service/common/src/ohosTest/resources'))
      .add(renderTmp('/module/fa/service/common/src/ohosTest/config.json'))
    )
  )
  .add(renderTmp('/module/fa/service/emptyAbility/api10/build-profile.json5'))
  .add(copyTmp('/entry/fa/service/emptyAbility/api10/hvigorfile.ts'))
  .add(renderTmp('/entry/fa/service/emptyAbility/api10/oh-package.json5'));

moduleBuilder.add(emptyAbilityOption, entryAbilityTemplate, initFun);

/**
 * NativeC
 *
 * @type {ModuleTemplateOption}
 */
const nativeAbilityOption: ModuleTemplateOption = ModuleTemplateOptionBuilder.builder()
  .abilityType(AbilityType.AtomicService)
  .apiType(APIType.FA)
  .apiVersion(APIVersion.API10)
  .language(LanguageType.ArkTs)
  .moduleTemplateType(ModuleTemplateType.NativeC)
  .runtimeOS(RuntimeOSType.HarmonyOS)
  .build();

const nativeAbilityTemplate: DirTemplateCreator = (dirName) => dirTmp(dirName)
  .add(dirTmp('libs')
    .add(dirTmp('armeabi-v7a'))
  )
  .add(dirTmp('src')
    .add(dirTmp('main')
      .add(dirTmp('cpp')
        .add(dirTmp('types')
          .add(dirTmp('libapplication')
            .add(copyTmp('module/fa/service/nativeApplication/api10/src/main/cpp/types/libapplication/index.d.ts'))
            // eslint-disable-next-line max-len
            .add(renderTmp('module/fa/service/nativeApplication/api10/src/main/cpp/types/libapplication/oh-package.json5'))
          )
        )
        .add(renderTmp('module/fa/app/nativeApplication/api10/src/main/cpp/CMakeLists.txt'))
        .add(renderTmp('module/fa/app/nativeApplication/api10/src/main/cpp/hello.cpp'))
      )
      .add(copyTmp('/module/fa/service/common/src/main/ets'))
      .add(dirTmp('js')
        .add(dirTmp('widget')
          .add(copyTmp('/module/fa/service/common/src/main/js/widget/common'))
          .add(copyTmp('/module/fa/service/common/src/main/js/widget/i18n'))
          .add(dirTmp('pages')
            .add(dirTmp('index')
              .add(copyTmp('/module/fa/service/common/src/main/js/widget/pages/index/index.css'))
              .add(copyTmp('/module/fa/service/common/src/main/js/widget/pages/index/index.hml'))
              .add(renderTmp('/module/fa/service/common/src/main/js/widget/pages/index/index.json'))
            )
          )
        )
      )
      .add(copyTmp('/module/fa/service/common/src/main/resources'))
      .add(renderTmp('/module/fa/service/nativeApplication/api10/src/main/config.json'))
    )
    .add(dirTmp('ohosTest')
      .add(copyTmp('/module/fa/service/common/src/ohosTest/ets'))
      .add(copyTmp('/module/fa/service/common/src/ohosTest/resources'))
      .add(renderTmp('/module/fa/service/common/src/ohosTest/config.json'))
    )
  )
  .add(renderTmp('/module/fa/service/nativeApplication/api10/build-profile.json5'))
  .add(copyTmp('/module/fa/service/nativeApplication/api10/hvigorfile.ts'))
  .add(renderTmp('/module/fa/service/nativeApplication/api10/oh-package.json5'));

const nativeCInitFun: ModuleConfigInitFun = async (name, option, template) => {
  template.node('src').node('main').node('cpp').node('types')
    .node('libapplication').node('oh-package.json5').data = { moduleName: name };
  template.node('src').node('main').node('cpp').node('CMakeLists.txt').data = { moduleName: name };
  template.node('src').node('main').node('cpp').node('hello.cpp').data = { moduleName: name };
  template.node('src').node('main').node('js').node('widget')
    .node('pages').node('index').node('index.json').data = { moduleName: name };
  template.node('src').node('main').node('config.json').data = { moduleName: name };
  template.node('src').node('ohosTest').node('config.json').data = { moduleName: name };
  template.node('build-profile.json5').data = { apiType: option.apiType };
  template.node('oh-package.json5').data = { moduleName: name };
};

moduleBuilder.add(nativeAbilityOption, nativeAbilityTemplate, nativeCInitFun);

/**
 * NativeC(entry)
 *
 * @type {ModuleTemplateOption}
 */
const nativeAbilityEntryOption: ModuleTemplateOption = ModuleTemplateOptionBuilder.builder()
  .abilityType(AbilityType.AtomicService)
  .apiType(APIType.FA)
  .apiVersion(APIVersion.API10)
  .language(LanguageType.ArkTs)
  .moduleTemplateType(ModuleTemplateType.NativeC)
  .runtimeOS(RuntimeOSType.HarmonyOS)
  .build();

const nativeAbilityEntryTemplate: DirTemplateCreator = (dirName) => dirTmp(dirName)
  .add(dirTmp('libs')
    .add(dirTmp('armeabi-v7a'))
  )
  .add(dirTmp('src')
    .add(dirTmp('main')
      .add(dirTmp('cpp')
        .add(dirTmp('types')
          .add(dirTmp('libentry')
            .add(copyTmp('module/fa/service/nativeApplication/api10/src/main/cpp/types/libapplication/index.d.ts'))
            // eslint-disable-next-line max-len
            .add(renderTmp('module/fa/service/nativeApplication/api10/src/main/cpp/types/libapplication/oh-package.json5'))
          )
        )
        .add(renderTmp('module/fa/app/nativeApplication/api10/src/main/cpp/CMakeLists.txt'))
        .add(renderTmp('module/fa/app/nativeApplication/api10/src/main/cpp/hello.cpp'))
      )
      .add(copyTmp('/module/fa/service/common/src/main/ets'))
      .add(dirTmp('js')
        .add(dirTmp('widget')
          .add(copyTmp('/module/fa/service/common/src/main/js/widget/common'))
          .add(copyTmp('/module/fa/service/common/src/main/js/widget/i18n'))
          .add(dirTmp('pages')
            .add(dirTmp('index')
              .add(copyTmp('/module/fa/service/common/src/main/js/widget/pages/index/index.css'))
              .add(copyTmp('/module/fa/service/common/src/main/js/widget/pages/index/index.hml'))
              .add(renderTmp('/module/fa/service/common/src/main/js/widget/pages/index/index.json'))
            )
          )
        )
      )
      .add(copyTmp('/module/fa/service/common/src/main/resources'))
      .add(renderTmp('/entry/fa/service/nativeApplication/api10/src/main/config.json'))
    )
    .add(dirTmp('ohosTest')
      .add(copyTmp('/module/fa/service/common/src/ohosTest/ets'))
      .add(copyTmp('/module/fa/service/common/src/ohosTest/resources'))
      .add(renderTmp('/entry/fa/service/nativeApplication/api10/src/ohosTest/config.json'))
    )
  )
  .add(renderTmp('/entry/fa/service/nativeApplication/api10/build-profile.json5'))
  .add(copyTmp('/module/fa/service/nativeApplication/api10/hvigorfile.ts'))
  .add(renderTmp('/entry/fa/service/nativeApplication/api10/oh-package.json5'));

const nativeCEntryInitFun: ModuleConfigInitFun = async (name, option, template) => {
  template.node('src').node('main').node('cpp').node('types')
    .node('libentry').node('oh-package.json5').data = { moduleName: name };
  template.node('src').node('main').node('cpp').node('CMakeLists.txt').data = { moduleName: name };
  template.node('src').node('main').node('cpp').node('hello.cpp').data = { moduleName: name };
  template.node('src').node('main').node('js').node('widget')
    .node('pages').node('index').node('index.json').data = { moduleName: 'myapplication' };
  template.node('src').node('main').node('config.json').data = { moduleName: name };
  template.node('src').node('ohosTest').node('config.json').data = { moduleName: name };
  template.node('build-profile.json5').data = { apiType: option.apiType };
  template.node('oh-package.json5').data = { moduleName: name };
};

entryBuilder.add(nativeAbilityEntryOption, nativeAbilityEntryTemplate, nativeCEntryInitFun);

/**
 * StaticLibrary
 *
 * @type {ModuleTemplateOption}
 */
const staticLibraryOption: ModuleTemplateOption = ModuleTemplateOptionBuilder.builder()
  .abilityType(AbilityType.AtomicService)
  .apiType(APIType.FA)
  .apiVersion(APIVersion.API10)
  .language(LanguageType.ArkTs)
  .moduleTemplateType(ModuleTemplateType.StaticLibrary)
  .runtimeOS(RuntimeOSType.HarmonyOS)
  .build();

const staticLibraryTemplate: DirTemplateCreator = (dirName) => dirTmp(dirName)
  .add(dirTmp('src')
    .add(dirTmp('main')
      .add(copyTmp('/module/fa/service/staticLibrary/api10/src/main/ets'))
      .add(copyTmp('/module/fa/service/staticLibrary/api10/src/main/resources'))
      .add(renderTmp('/module/fa/service/staticLibrary/api10/src/main/config.json'))
    )
  )
  .add(renderTmp('/module/fa/service/staticLibrary/api10/build-profile.json5'))
  .add(copyTmp('/module/fa/service/staticLibrary/api10/hvigorfile.ts'))
  .add(copyTmp('/module/fa/service/staticLibrary/api10/Index.ets'))
  .add(renderTmp('/module/fa/service/staticLibrary/api10/oh-package.json5'));
const staticLibraryInitFun: ModuleConfigInitFun = async (name, option, template) => {
  template.node('src').node('main').node('config.json').data = { moduleName: name };
  template.node('build-profile.json5').data = { apiType: option.apiType };
  template.node('oh-package.json5').data = { moduleName: name };
};

moduleBuilder.add(staticLibraryOption, staticLibraryTemplate, staticLibraryInitFun);

/**
 * StaticLibraryWithC
 *
 * @type {ModuleTemplateOption}
 */
const staticLibraryWithCOption: ModuleTemplateOption = ModuleTemplateOptionBuilder.builder()
  .abilityType(AbilityType.AtomicService)
  .apiType(APIType.FA)
  .apiVersion(APIVersion.API10)
  .language(LanguageType.ArkTs)
  .moduleTemplateType(ModuleTemplateType.StaticLibraryWithC)
  .runtimeOS(RuntimeOSType.HarmonyOS)
  .build();

const staticLibraryWithCTemplate: DirTemplateCreator = (dirName) => dirTmp(dirName)
  .add(dirTmp('libs')
    .add(dirTmp('armeabi-v7a'))
  )
  .add(dirTmp('src')
    .add(dirTmp('main')
      .add(dirTmp('cpp')
        .add(dirTmp('types')
          .add(dirTmp('liblibrary')
            .add(copyTmp('module/fa/service/nativeApplication/api10/src/main/cpp/types/libapplication/index.d.ts'))
            // eslint-disable-next-line max-len
            .add(renderTmp('module/fa/service/nativeApplication/api10/src/main/cpp/types/libapplication/oh-package.json5'))
          )
        )
        .add(renderTmp('module/fa/service/staticLibraryWithC/api10/src/main/cpp/CMakeLists.txt'))
        .add(renderTmp('module/fa/app/nativeApplication/api10/src/main/cpp/hello.cpp'))
      )
      .add(copyTmp('/module/fa/service/staticLibraryWithC/api10/src/main/ets'))
      .add(copyTmp('/module/fa/service/staticLibrary/api10/src/main/resources'))
      .add(renderTmp('/module/fa/service/staticLibrary/api10/src/main/config.json'))
    )
  )
  .add(renderTmp('/module/fa/service/staticLibraryWithC/api10/build-profile.json5'))
  .add(copyTmp('/module/fa/service/staticLibrary/api10/hvigorfile.ts'))
  .add(copyTmp('/module/fa/service/staticLibrary/api10/Index.ets'))
  .add(renderTmp('/module/fa/service/staticLibraryWithC/api10/oh-package.json5'));
const staticLibraryWithCInitFun: ModuleConfigInitFun = async (name, option, template) => {
  template.node('src').node('main').node('cpp').node('types').node('liblibrary')
    .node('oh-package.json5').data = { moduleName: name };
  template.node('src').node('main').node('cpp').node('CMakeLists.txt').data = { moduleName: name };
  template.node('src').node('main').node('cpp').node('hello.cpp').data = { moduleName: name };
  template.node('src').node('main').node('config.json').data = { moduleName: name };
  template.node('build-profile.json5').data = { apiType: option.apiType };
  template.node('oh-package.json5').data = { moduleName: name };
};

moduleBuilder.add(staticLibraryWithCOption, staticLibraryWithCTemplate, staticLibraryWithCInitFun);
